package com.linkvin.ec.hcs.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.linkvin.ec.hcs.dao.model.mdm.bo.WarehouseLocationBo;
import com.linkvin.ec.hcs.service.mdm.WarehouseLocationService;
import com.linkvin.ec.hcs.service.util.enums.WarehouseLocTypeEnum;
import com.linkvin.ec.hcs.util.EnumUtil;
import com.linkvin.ec.hcs.vo.EnumVO;
import com.linkvin.ec.hcs.vo.RestVO;
import com.linkvin.ec.hcs.vo.WarehouseLocationVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/warehouseLocation", produces = MediaType.APPLICATION_JSON_VALUE)
public class WarehouseLocationCotroller {

    private static final Logger LOGGER = LoggerFactory.getLogger(WarehouseController.class);

    @Autowired
    private WarehouseLocationService warehouseLocationService;

    @PostMapping(value = "/queryPage")
    public RestVO<Page<WarehouseLocationVO>> queryWarehouseLocList(@RequestBody Page<WarehouseLocationBo> page){

        RestVO<Page<WarehouseLocationVO>> result = new RestVO<>(0,"success");
        LOGGER.info("WarehouseLocationCotroller.queryWarehouseLocList params : {}", page);
        try{

            Page<WarehouseLocationVO> pageWarehouseLocVO = new Page<>();
            page = warehouseLocationService.queryPageWarehouseLoc(page);
            BeanUtils.copyProperties(page, pageWarehouseLocVO);

            List<WarehouseLocationVO> listWarehouseLocVO = Lists.newArrayList();
            List<WarehouseLocationBo> listWarehouseLocBo = page.getRecords();

            listWarehouseLocBo.forEach(warehouseLocation -> {
                WarehouseLocationVO warehouseLocationVO = new WarehouseLocationVO();
                BeanUtils.copyProperties(warehouseLocation,warehouseLocationVO);
                warehouseLocationVO.setLocType(WarehouseLocTypeEnum.getByValue(warehouseLocationVO.getLocType()).getText());
                listWarehouseLocVO.add(warehouseLocationVO);
            });

            pageWarehouseLocVO.setRecords(listWarehouseLocVO);
            result.setData(pageWarehouseLocVO);

        }catch(Exception ex)
        {
            LOGGER.error("WarehouseLocationCotroller.queryWarehouseLocList ERROR Message : {} ", ex.getMessage());
            result.setCode(30004);
            result.setMessage("仓库库位查询错误");
        }


        return result;
    }

    @GetMapping(value = "/queryWhLocType")
    public RestVO<List<EnumVO>> queryWhLocType() {

        RestVO<List<EnumVO>> result = new RestVO<>(0,"success");
        Map<String,String> lookup = WarehouseLocTypeEnum.lookup();
        EnumUtil enumUtil =new EnumUtil();
        List<EnumVO> lists = enumUtil.getListEnumVO(lookup);

        result.setData(lists);
        return  result;
    }

    @PostMapping(value = "/create")
    public RestVO<WarehouseLocationVO> create(@RequestBody WarehouseLocationBo warehouseLocationBo){

        RestVO<WarehouseLocationVO> result = new RestVO<>(0,"success");

        LOGGER.info("WarehouseLocationCotroller.create params : {}", warehouseLocationBo);
        try {

            WarehouseLocationVO warehouseLocVO =new WarehouseLocationVO();
            warehouseLocationBo = warehouseLocationService.addWarehouseLoc(warehouseLocationBo);
            BeanUtils.copyProperties(warehouseLocationBo,warehouseLocVO);
            warehouseLocVO.setLocType(WarehouseLocTypeEnum.getByValue(warehouseLocVO.getLocType()).getText());
            result.setData(warehouseLocVO);

        }catch (Exception e){
            LOGGER.error("WarehouseLocationCotroller.create ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }

        return result;
    }

    @PostMapping(value = "/update")
    public RestVO<WarehouseLocationVO> update(@RequestBody WarehouseLocationBo warehouseLocationBo){
        RestVO<WarehouseLocationVO> result = new RestVO<>(0,"success");

        LOGGER.info("WarehouseLocationCotroller.update params : {}", warehouseLocationBo);

        try {

            WarehouseLocationVO warehouseLocationVO =new WarehouseLocationVO();
            warehouseLocationBo = warehouseLocationService.updateWarehouseLoc(warehouseLocationBo);
            BeanUtils.copyProperties(warehouseLocationBo,warehouseLocationVO);
            warehouseLocationVO.setLocType(WarehouseLocTypeEnum.getByValue(warehouseLocationVO.getLocType()).getText());
            result.setData(warehouseLocationVO);
        }catch (Exception e){
            LOGGER.error("WarehouseLocationCotroller.update ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }

        return result;
    }

    @PostMapping(value = "/delete")
    public RestVO<Boolean> deleteWarehouse(@RequestBody List<Integer> ids){

        RestVO<Boolean> result = new RestVO<>(0,"success");
        LOGGER.info("WarehouseController.deleteWarehouse params : {}", ids);

        try {
            int i =  warehouseLocationService.deleteWarehouseLoc(ids);
            Boolean blnrtn = i > 0;
            result.setData(blnrtn);
        }catch (Exception e){
            LOGGER.error("WarehouseController.deleteWarehouse ERROR Message : {} ", e.getMessage());
            result.setData(false);
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return  result;
    }


}
